package com.rocket.bedroom.security;

import com.rocket.bedroom.utils.SpringUtils;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Set;

/**
 * @Author: 周毅
 * @ClassName:
 * @Description: TODO
 * @Date: Create in 14:51 2020/6/14
 * @ModifyBy:
 */


public class RedisShrioCache<K, V> implements Cache<K, V>
{
    /*@Resource
    private RedisTemplate<String,Object> redisTemplate;*/

    @Override
    public V get(K k) throws CacheException
    {
        StringRedisTemplate redisTemplate = SpringUtils.getBean(StringRedisTemplate.class);
        //将redis序列化方式换成 String字符串
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        return (V) redisTemplate.opsForValue().get(k);
    }

    @Override
    public V put(K k, V v) throws CacheException
    {
        RedisTemplate<String, Object> redisTemplate = SpringUtils.getBean(RedisTemplate.class);

        //将redis序列化方式换成 String字符串
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.opsForValue().set(k.toString(), v);

        return null;
    }

    @Override
    public V remove(K k) throws CacheException
    {
        return null;
    }

    @Override
    public void clear() throws CacheException
    {

    }

    @Override
    public int size()
    {
        return 0;
    }

    @Override
    public Set<K> keys()
    {
        return null;
    }

    @Override
    public Collection<V> values()
    {
        return null;
    }
}
